package cn.iocoder.yudao.module.system.controller.admin.seatreserve;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.seatreserve.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.seatreserve.SeatReserveDO;
import cn.iocoder.yudao.module.system.service.seatreserve.SeatReserveService;

@Tag(name = "管理后台 - 座位预定信息")
@RestController
@RequestMapping("/system/seat-reserve")
@Validated
@Slf4j
public class SeatReserveController {

    @Resource
    private SeatReserveService seatReserveService;

    @PostMapping("/create")
    @Operation(summary = "创建座位预定信息")
    @PreAuthorize("@ss.hasPermission('system:seat-reserve:create')")
    public CommonResult<Map<String, Object>> createSeatReserve(@Valid @RequestBody SeatReserveSaveReqVO createReqVO) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            seatReserveService.createSeatReserve(createReqVO);
            resultMap.put("success",0);
        } catch (RuntimeException e) {
            resultMap.put("success",1);
            // 处理异常，例如用户提示或记录日志
            resultMap.put("errMsg",e.getMessage());
            log.info("捕获到异常：" + e.getMessage());
        }
        return success(resultMap);
    }

    @PutMapping("/update")
    @Operation(summary = "更新座位预定信息")
    @PreAuthorize("@ss.hasPermission('system:seat-reserve:update')")
    public CommonResult<Boolean> updateSeatReserve(@Valid @RequestBody SeatReserveSaveReqVO updateReqVO) {
        seatReserveService.updateSeatReserve(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除座位预定信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:seat-reserve:delete')")
    public CommonResult<Boolean> deleteSeatReserve(@RequestParam("id") Long id) {
        seatReserveService.deleteSeatReserve(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得座位预定信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:seat-reserve:query')")
    public CommonResult<SeatReserveRespVO> getSeatReserve(@RequestParam("id") Long id) {
        SeatReserveDO seatReserve = seatReserveService.getSeatReserve(id);
        return success(BeanUtils.toBean(seatReserve, SeatReserveRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得座位预定信息分页")
    @PreAuthorize("@ss.hasPermission('system:seat-reserve:query')")
    public CommonResult<PageResult<SeatReserveRespVO>> getSeatReservePage(@Valid SeatReservePageReqVO pageReqVO) {
        PageResult<SeatReserveDO> pageResult = seatReserveService.getSeatReservePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SeatReserveRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得座位预定信息列表")
    @PreAuthorize("@ss.hasPermission('system:seat-reserve:query')")
    public CommonResult<List<SeatReserveDO>> getSeatReserveList(@Valid SeatReservePageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SeatReserveDO> pageResult = seatReserveService.getSeatReservePage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, SeatReserveDO.class));

    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出座位预定信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:seat-reserve:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSeatReserveExcel(@Valid SeatReservePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SeatReserveDO> list = seatReserveService.getSeatReservePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "座位预定信息.xls", "数据", SeatReserveRespVO.class,
                        BeanUtils.toBean(list, SeatReserveRespVO.class));
    }

}